package com.market.xg.gateway.conf;

import com.market.xg.common.starter.util.CommonUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "auth")
public class AuthProperties {
    /**
     * 黑名单
     */
    private List<String> blackUris;
    /**
     * 白名单
     */
    private List<String> whiteUris;

    /**
     * 不需要鉴权uri
     */
    private List<String> unAuthUris;

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    private boolean isAntMatch(List<String> uris, String uri) {
        return CommonUtils.isNotEmpty(uris) && uris.stream().anyMatch(pateter -> antPathMatcher.match(pateter, uri));
    }

    public boolean isBlackUri(String uri){
        return isAntMatch(blackUris,uri);
    }
    public boolean isWhiteUri(String uri){
        return isAntMatch(whiteUris,uri);
    }

    /**
     * 不需要鉴权的uri
     * @param uri
     * @return
     */
    public boolean isUnAuthUri(String uri){
        return isAntMatch(unAuthUris,uri);
    }
}
